package com.sc.nft.entity.congestive;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.entity.*;
import com.sc.nft.enums.UserEquityPropsGetTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 赠送的充血模型
 */
@Data
public class GiftCT {
    @ApiModelProperty("赠送的藏品id")
    private List<Long> giftId;

    @ApiModelProperty("支付密码")
    private String paymentPass;

    @ApiModelProperty("赠送地址")
    private String address;

    @ApiModelProperty("支付方式  1.星球碎片 2.余额 3.生态燃料")
    private Integer consumeType;

    @ApiModelProperty("赠送用户")
    private UserInfo userInfo;

    @ApiModelProperty("赠送人的钱包")
    private UserWallet userWallet;

    @ApiModelProperty("赠送人的星球碎片(积分)")
    private UserAssets userAssets;

    @ApiModelProperty("赠送人地址")
    private UserChainInfo giftUserChainInfo;

    @ApiModelProperty("用户真实持有的")
    private List<UserEquityProps> userEquityProps;

    @ApiModelProperty("赠送用户")
    private UserInfo complimentarySubscriber;

    @ApiModelProperty("接受人用户数据")
    private UserChainInfo userChainInfo;

    @ApiModelProperty("接收人")
    private List<UserEquityProps> takeIn;

    @ApiModelProperty("所需碎片")
    private BigDecimal debris;

    @ApiModelProperty("所需钱")
    private BigDecimal amount;

    @ApiModelProperty("所需生态燃料")
    private BigDecimal ecology;


    public Boolean passwordAuthentification() {
        if (paymentPass.equals(userInfo.getTransactionPassword())) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    /**
     * 接受赠送数据落库所需
     */
    public void takeInGift() {
        this.takeIn.forEach(equityProps -> {
            equityProps.setUserId(complimentarySubscriber.getId());
            equityProps.setGetType(UserEquityPropsGetTypeEnum.RECEPTION_CONVERSION);
            equityProps.setPrimeType(ObjectUtil.isNull(equityProps) ? UserEquityPropsGetTypeEnum.RECEPTION_CONVERSION : equityProps.getPrimeType());
        });
    }


    public void calculate(List<PropGift> list, Integer sum) {
        BigDecimal deb = BigDecimal.ZERO;
        BigDecimal amo = BigDecimal.ZERO;
        BigDecimal ecology = BigDecimal.ZERO;
        for (PropGift propGift : list) {
            if (propGift.getConsumeType().equals(1)) {
                deb = deb.add(Convert.toBigDecimal(propGift.getQuantityConsume()).multiply(Convert.toBigDecimal(sum)));
            }

            else if (propGift.getConsumeType().equals(2)) {
                amo = amo.add(Convert.toBigDecimal(propGift.getQuantityConsume()).multiply(Convert.toBigDecimal(sum)));
            }

            else if (propGift.getConsumeType().equals(3)){
                ecology = ecology.add(Convert.toBigDecimal(propGift.getQuantityConsume()).multiply(Convert.toBigDecimal(sum)));
            }
        }
        this.debris = deb;
        this.amount = amo;
        this.ecology = ecology;
    }


    public Boolean selfDenial(){
        if (this.giftUserChainInfo.getAddress().equals(this.address)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
